<?php
/**
 * 支付接口调测例子
 * ================================================================
 * index 进入口，方法中转
 * submitOrderInfo 提交订单信息
 * queryOrder 查询订单
 * 
 * ================================================================
 */
require('Utils.php');
require('config/config.php');
require('lib/RequestHandler.php');
require('lib/ClientResponseHandler.php');
require('lib/PayHttpClient.php');

Class Swiftpay{
    private $resHandler = null;
    private $reqHandler = null;
    private $pay = null;
    private $cfg = null;
    private $error = null;

    public function __construct() {
        $this->Request();
    }

    public function Request() {
        $this->resHandler = new ClientResponseHandler();
        $this->reqHandler = new RequestHandler();
        $this->pay = new PayHttpClient();
        $this->cfg = new Config();

        $this->reqHandler->setGateUrl($this->cfg->C('url'));
    }

    /**
     * 提交订单请求信息
     */
    public function submit($orderInfo, $key)
    {
        $this->reqHandler->setReqParams($orderInfo);
        $this->reqHandler->setKey($key);
        $this->reqHandler->setParameter('version',$this->cfg->C('version'));
        $this->reqHandler->setParameter('charset',$this->cfg->C('charset'));
        $this->reqHandler->setParameter('mch_create_ip',$_SERVER['REMOTE_ADDR']); //终端ip
        $this->reqHandler->setParameter('nonce_str', (string)(mt_rand(time(),time()+rand())));//随机字符串，必填项，不长于 32 位
        $this->reqHandler->createSign();//创建签名
        $data = Utils::toXml($this->reqHandler->getAllParameters());
        $this->pay->setReqContent($this->reqHandler->getGateURL(),$data);

        // 发送请求
        if (!$this->pay->call()) {
            $this->setError('Response Code:'.$this->pay->getResponseCode().' Error Info:'.$this->pay->getErrInfo().'3');
            return false;
        }

        $this->resHandler->setContent($this->pay->getResContent());
        $this->resHandler->setKey($this->reqHandler->getKey());

        // 验证返回的签名 是否一致
        if(! $this->resHandler->isTenpaySign()) {
            $this->setError('Error Code:'.$this->resHandler->getParameter('status').' Error Message:'.$this->resHandler->getParameter('message').'2');
            return false;
        }

        //当返回状态与业务结果都为0时才返回支付二维码，其它结果请查看接口文档
        if($this->resHandler->getParameter('status') != 0 || $this->resHandler->getParameter('result_code') != 0){
            $this->setError('Error Code:'.$this->resHandler->getParameter('err_code').' Error Message:'.$this->resHandler->getParameter('err_msg').'1');
            return false;
        }
        return $this->resHandler->getAllParameters();
    }

    /**
     * 提供给威富通的回调方法
     */
    public function callback($key){
        $this->resHandler->setKey($key);
        if($this->resHandler->isTenpaySign()){
            if($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0){
                return $this->resHandler->getAllParameters();
            }else{
                $this->setError($this->resHandler->getAllParameters());
                return false;
            }
        }else{
            $this->setError($xml);
            return false;
        }
    }

    /**
     * 获取商户编号
     */
    public function getOutTradeNo()
    {
        $xml = file_get_contents('php://input');
        $this->resHandler->setContent($xml);
        return $this->resHandler->getParameter('out_trade_no');
    }

    private function setError($msg)
    {
        $this->error = $msg;
    }

    public function getError()
    {
        return $this->error;
    }
}
?>